﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ttlang.compiler;

namespace  ttlang.semantic
{
    public class TTVar : IMember
    {
    
        public TTVar(IContainer container, string name)
        {
            this.container = container;
            this.name = name;
        }
        public string name
        {
            get;
            private set;
        }
        public string[] modifers;//将来改成枚举
        public node.Node_Expr_Call[] attributes;//
        public src.AstNode notParseType;
        public src.AstNode notParseInitValue;

        public node.Node_Expr initvalue;
        public TTType VarType;
        public TTType tttype
        {
            get
            {
                return VarType;
            }
        }
        public IContainer container
        {
            get;
            private set;
        }


        public MEMBERTYPE type => MEMBERTYPE.VAR;

        public string fullname
        {
            get
            {
                if (container != null)
                {
                    return container.fullname + "." + name;
                }
                else
                {
                    return name;
                }
            }
        
            
        }
        public static TTVar Create(CompilerContext context, IContainer _container, src.AstNode innode)
        {
            string name = "";
            int childstart = 0;

            //0 id
            //1 {
            //end }
            List<string> modifers = new List<string>();
            List<node.Node_Expr_Call> attrs = new List<node.Node_Expr_Call>();

            for (var i = 0; i < innode.children.Count; i++)
            {
                if (innode.children[i].type == "modifiers")
                {
                    //处理修饰器
                    var mod = innode.children[i];
                    for (var j = 0; j < mod.children.Count; j++)
                    {
                        if (mod.children[j].text == "@")
                        {
                            var next = mod.children[j + 1];
                            compiler.compiler_expr.ParseNode_Expr_Call(context, next, out node.Node_Expr_Call attr);
                            attrs.Add(attr);
                            j++;
                        }
                        else
                        {
                            modifers.Add(mod.children[j].text);
                        }
                    }

                }
                if (innode.children[i].type == "ID")
                {
                    name = innode.children[i].text;
                    childstart = i + 1; //skip '('

                    //args
                    break;
                }
            }

            semantic.TTVar _var = new semantic.TTVar(_container, name);
            _var.modifers = modifers.ToArray();
            _var.attributes = attrs.ToArray();
            //第一遍不需要深入处理
            if (innode.children[childstart].text == ":")//withtype
            {
                _var.notParseType = innode.children[childstart + 1];
                childstart += 2;
            }
            if (innode.children[childstart].text == "=")//withvalue
            {
                _var.notParseInitValue = innode.children[childstart + 1];
            }
            return _var;
        }
    }
}
